package cn.jsj.dao;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import cn.jsj.pojo.TblBankAccount;
import cn.jsj.pojo.TblCashAccount;

/**
 * 对现金账的各种处理类
 * @author LI
 *
 */

public class TblCashAccountDAO  {

	/**
	 * 获得现金账信息条数
	 * @return：现金账信息总条数
	 */
	public int getCashAccountNumb(){
		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="select count(*) from TblCashAccount tca";
	    List result=session.createQuery(hql)
		.list();
		// 提交事务
		tx.commit();
		session.close();
		//获得结果的迭代器
		 ListIterator iterator = result.listIterator();
		 Long rows =(Long)iterator.next();
		 return Integer.parseInt(rows.toString());
	}

	/**
	 * 获得某个页面的现金账信息
	 * @param startNumb：从第几条开始
	 * @param totalNumb：总共几条
	 * @return：现金账信息队列
	 */
	public java.util.List<TblCashAccount> getCashAccount(int startNumb,int totalNumb){
		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblCashAccount.class)
		.addOrder(org.hibernate.criterion.Order.desc("cashAccTime"))
		.setFirstResult(startNumb)
		.setMaxResults(totalNumb)
		.list();
		// 提交事务
		tx.commit();
		session.close();
		return result;
	}

	/**
	 * 获得符合搜索条件的现金账条数
	 * @param sbp：后台与DB进行交互的对象
	 * @return：现金账信息条数
	 */
	public int getSearchCashNumb(cn.jsj.bgtodb.SearchCashProperty scp){

		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=new LinkedList();
		//如果用户没有输入月份
		if(scp.getCashMonth().length()==0){
			//根据输入的字符串构造出要搜索的时间段
			String begin=scp.getCashYear()+"-01"+"-01";
			java.util.Date beginDate=cn.jsj.utils.TimeDispose.transferDate(begin);
			String end=scp.getCashYear()+"-12"+"-31";
			java.util.Date endDate=cn.jsj.utils.TimeDispose.transferDate(end);
			//查询在这个时间段记录的条数
			String hql="select count(*) from TblCashAccount tca where tca.cashAccTime between?and?";
		    result=session.createQuery(hql)
			.setDate(0, beginDate)
			.setDate(1, endDate)
			.list();
		    // 提交事务
			tx.commit();
			session.close();

			//获得结果的迭代器
			 ListIterator iterator = result.listIterator();
			 Long rows =(Long)iterator.next();
			 return Integer.parseInt(rows.toString());

		}
		//如果输入了月份
		else{
			//根据输入的字符串构造出要搜索的时间段
			String begin=scp.getCashYear()+"-"+scp.getCashMonth()+"-01";
			java.util.Date beginDate=cn.jsj.utils.TimeDispose.transferDate(begin);
			//判断这个月有没有31号
			int month=Integer.parseInt(scp.getCashMonth());
			//结束日子
			String end="";
			if(month<8&&month%2==1){
				 end=scp.getCashYear()+"-"+scp.getCashMonth()+"-31";
			}
			if(month<8&&month%2==0)
			{
				 end=scp.getCashYear()+"-"+scp.getCashMonth()+"-30";
			}
			if(month>=8&&month%2==0){
				 end=scp.getCashYear()+"-"+scp.getCashMonth()+"-31";
			}
			if(month>=8&&month%2==1){
				end=scp.getCashYear()+"-"+scp.getCashMonth()+"-30";
			}
			java.util.Date endDate=cn.jsj.utils.TimeDispose.transferDate(end);
			//查询在这个时间段记录的条数
			String hql="select count(*) from TblCashAccount tca where tca.cashAccTime between?and?";
		    result=session.createQuery(hql)
			.setDate(0, beginDate)
			.setDate(1, endDate)
			.list();
		    // 提交事务
			tx.commit();
			session.close();

			//获得结果的迭代器
			 ListIterator iterator = result.listIterator();
			 Long rows =(Long)iterator.next();
			 return Integer.parseInt(rows.toString());
		}
		}



	/**
	 * 获得符合搜索条件的现金账信息
	 * @param scp：后台与DB进行交互的对象
	 * @return：现金账信息队列
	 */
	public java.util.List<TblCashAccount> getSearchCashInfo(cn.jsj.bgtodb.SearchCashProperty scp){

		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		//搜索结果队列
		//判断是否输入了月份
		List result=new LinkedList();
		if(scp.getCashMonth().length()==0){
			String begin=scp.getCashYear()+"-01"+"-01";
			java.util.Date beginDate=cn.jsj.utils.TimeDispose.transferDate(begin);
			String end=scp.getCashYear()+"-12"+"-31";
			java.util.Date endDate=cn.jsj.utils.TimeDispose.transferDate(end);

			result=session.createCriteria(TblCashAccount.class)
			.add(org.hibernate.criterion.Restrictions.between("cashAccTime", beginDate, endDate))
			.addOrder(org.hibernate.criterion.Order.desc("cashAccTime"))
			.setFirstResult(scp.getStartNumb())
			.setMaxResults(scp.getTotalNumb())
			.list();
		}else{
			String begin=scp.getCashYear()+"-"+scp.getCashMonth()+"-01";
			java.util.Date beginDate=cn.jsj.utils.TimeDispose.transferDate(begin);
			//判断这个月有没有31号
			int month=Integer.parseInt(scp.getCashMonth());

			//结束日子
			String end="";
			if(month<8&&month%2==1){
				 end=scp.getCashYear()+"-"+scp.getCashMonth()+"-31";
			}
			if(month<8&&month%2==0)
			{
				 end=scp.getCashYear()+"-"+scp.getCashMonth()+"-30";
			}
			if(month>=8&&month%2==0){
				 end=scp.getCashYear()+"-"+scp.getCashMonth()+"-31";
			}
			if(month>=8&&month%2==1){
				end=scp.getCashYear()+"-"+scp.getCashMonth()+"-30";
			}
			java.util.Date endDate=cn.jsj.utils.TimeDispose.transferDate(end);
			result=session.createCriteria(TblCashAccount.class)
			 .add(org.hibernate.criterion.Restrictions.between("cashAccTime", beginDate, endDate))
			 .addOrder(org.hibernate.criterion.Order.desc("cashAccTime"))
				.setFirstResult(scp.getStartNumb())
				.setMaxResults(scp.getTotalNumb())
				.list();
		}
		// 提交事务
		tx.commit();
		session.close();
			return result;
		}


	/**
	 * 保存一条现金账信息
	 * @param tba：现金账信息对象
	 * @return：1：保存成功
	 */
	public int saveCashInfo(cn.jsj.pojo.TblCashAccount tca){

		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		session.save(tca);
		// 提交事务
		tx.commit();
		session.close();
		return 1;
	}


	/**
	 * 查看现金账报表信息
	 * @param rf：报表信息参数类
	 * @return：报表信息字符串：0:支出收入都为NULL
	 */
	public String getCashReportInfo(cn.jsj.bgtodb.ReportForm rf){
		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		//判断报表类型
		int catalogNumb=Integer.parseInt(rf.getCatalogName());
		//查询结果
		List result;
		//如果为年报表
		if(catalogNumb==1){
			String begin=rf.getDetailTime()+"-01"+"-01";
			java.util.Date beginDate=cn.jsj.utils.TimeDispose.transferDate(begin);
			String end=rf.getDetailTime()+"-12"+"-31";
			java.util.Date endDate=cn.jsj.utils.TimeDispose.transferDate(end);
			String hql="select sum(tca.incomeMoney),sum(tca.payoutMoney) from TblCashAccount tca where tca.cashAccTime between?and?";
		    result=session.createQuery(hql)
			.setDate(0, beginDate)
			.setDate(1, endDate)
			.list();
		    ListIterator iterator = result.listIterator();
		    Object[] rows = (Object[]) iterator.next();
		    //判断是否为NULL
		    if(rows[0]==null){
		    	return 0+"";
		    }
		    else{
		    	return rows[0].toString()+","+rows[1].toString();
		    }

		}
		//如果为季度报表
		if(catalogNumb==2){
			//判断为哪个季度
			int quarterLabel=Integer.parseInt(rf.getDetailTime());

			//开始结束时间
			java.util.Date beginDate=new java.util.Date();
			java.util.Date endDate=new java.util.Date();

			//如果为第一季度
			if(quarterLabel==1){
				String begin=cn.jsj.utils.TimeDispose.getYear()+"-01"+"-01";
				 beginDate=cn.jsj.utils.TimeDispose.transferDate(begin);
				String end=cn.jsj.utils.TimeDispose.getYear()+"-03"+"-31";
				 endDate=cn.jsj.utils.TimeDispose.transferDate(end);
			}
			if(quarterLabel==2){
				String begin=cn.jsj.utils.TimeDispose.getYear()+"-04"+"-01";
				 beginDate=cn.jsj.utils.TimeDispose.transferDate(begin);
				String end=cn.jsj.utils.TimeDispose.getYear()+"-06"+"-30";
				 endDate=cn.jsj.utils.TimeDispose.transferDate(end);
			}
			if(quarterLabel==3){
				String begin=cn.jsj.utils.TimeDispose.getYear()+"-07"+"-01";
				 beginDate=cn.jsj.utils.TimeDispose.transferDate(begin);
				String end=cn.jsj.utils.TimeDispose.getYear()+"-09"+"-30";
				 endDate=cn.jsj.utils.TimeDispose.transferDate(end);
			}
			if(quarterLabel==4){
				String begin=cn.jsj.utils.TimeDispose.getYear()+"-10"+"-01";
				 beginDate=cn.jsj.utils.TimeDispose.transferDate(begin);
				String end=cn.jsj.utils.TimeDispose.getYear()+"-12"+"-31";
				 endDate=cn.jsj.utils.TimeDispose.transferDate(end);
			}

			String hql="select sum(tca.incomeMoney),sum(tca.payoutMoney) from TblCashAccount tca where tca.cashAccTime between?and?";
		    result=session.createQuery(hql)
			.setDate(0, beginDate)
			.setDate(1, endDate)
			.list();
		    if(result.size()!=0){
		    ListIterator iterator = result.listIterator();
		    Object[] rows = (Object[]) iterator.next();
		    //判断是否为NULL
		    if(rows[0]==null){
		    	return 0+"";
		    }
		    else{
		    	return rows[0].toString()+","+rows[1].toString();
		    }
		    }
		}

		//如果为月报表
		if(catalogNumb==3){
			//获得月份
			int month=Integer.parseInt(rf.getDetailTime());

			String begin=cn.jsj.utils.TimeDispose.getYear()+"-"+rf.getDetailTime()+"-01";
			java.util.Date beginDate=cn.jsj.utils.TimeDispose.transferDate(begin);

			//结束日子
			String end="";
			if(month<8&&month%2==1){
				 end=cn.jsj.utils.TimeDispose.getYear()+"-"+rf.getDetailTime()+"-31";
			}
			if(month<8&&month%2==0)
			{
				 end=cn.jsj.utils.TimeDispose.getYear()+"-"+rf.getDetailTime()+"-30";
			}
			if(month>=8&&month%2==0){
				 end=cn.jsj.utils.TimeDispose.getYear()+"-"+rf.getDetailTime()+"-31";
			}
			if(month>=8&&month%2==1){
				end=cn.jsj.utils.TimeDispose.getYear()+"-"+rf.getDetailTime()+"-30";
			}
			java.util.Date endDate=cn.jsj.utils.TimeDispose.transferDate(end);

			String hql="select sum(tca.incomeMoney),sum(tca.payoutMoney) from TblCashAccount tca where tca.cashAccTime between?and?";
		    result=session.createQuery(hql)
			.setDate(0, beginDate)
			.setDate(1, endDate)
			.list();
		    if(result.size()!=0){
		    ListIterator iterator = result.listIterator();
		    Object[] rows = (Object[]) iterator.next();
		    //判断是否为NULL
		    if(rows[0]==null){
		    	return 0+"";
		    }
		    else{
		    	return rows[0].toString()+","+rows[1].toString();
		    }
		    }
		}
		// 提交事务
		tx.commit();
		session.close();
		return 0+"";
	}

	/**
	 * 获得现金帐总资金
	 * @return：总资金
	 */
	public int getTotalCashMoney(){
		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="select sum(tca.incomeMoney),sum(tca.payoutMoney) from TblCashAccount tca";
		List result=session.createQuery(hql).list();
		// 提交事务
		tx.commit();
		session.close();
		ListIterator iterator = result.listIterator();
	    Object[] rows = (Object[]) iterator.next();
	    if(rows[0]==null){
	    	return 0;
	    }else{
	    	int income=Integer.parseInt(rows[0].toString());
	    	int payout=Integer.parseInt(rows[1].toString());
	    	int leftMoney=income-payout;
	    	return leftMoney;
	    }
	}

	/**
	 * 获取时间段现金账资金情况
	 * @param stmp：参数类
	 * @return
	 */
	public String getTimeCashMoney(cn.jsj.bgtodb.SomeTimeMoneyProperty stmp){
		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="select sum(tca.incomeMoney),sum(tca.payoutMoney) from TblCashAccount tca where tca.cashAccTime between?and?";
		List result=session.createQuery(hql)
		.setDate(0, stmp.getStartTime())
		.setDate(1, stmp.getEndTime())
		.list();
		// 提交事务
		tx.commit();
		session.close();
		ListIterator iterator = result.listIterator();
	    Object[] rows = (Object[]) iterator.next();
	    if(rows[0]==null){
	    	return "0,0,0";
	    }else{
	    	int income=Integer.parseInt(rows[0].toString());
	    	int payout=Integer.parseInt(rows[1].toString());
	    	int leftMoney=income-payout;
	    	return income+","+payout+","+leftMoney;
	    }
	}

	/**
	 * 更新现金帐信息
	 * @param tba：现金帐信息对象
	 * @return：1：更新成功
	 */
	public int updateBankInfo(cn.jsj.pojo.TblCashAccount tca){

		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		session.update(tca);
		// 提交事务
		tx.commit();
		session.close();
		return 1;
	}
}